<template>
	<view class="page-body">
		<view class="top">
			<view class="item">
				<view class="left">收货人</view>
				<input type="text" v-model="addressData.consignee" maxlength="30" placeholder="请填写收货人姓名" />
			</view>
			<view class="item">
				<view class="left">手机号码</view>
				<input type="number" v-model="addressData.mobile" placeholder="请填写收货人手机号" />
			</view>
			<view class="item" @tap="region_show = true;">
				<view class="left">所在地区</view>
				<input disabled type="text" v-model="addressData.merger_name" placeholder="省市区县" />
				<text class="right-arrow"></text>
			</view>
			<view class="item address">
				<view class="left">详细地址</view>
				<textarea type="text"  maxlength="100" v-model="addressData.address" placeholder="详细地址：如道路、小区、栋楼号等" />
			</view>
			<view class="isdefault">
				<view class="left">
					<view class="set">设置默认地址</view>
					<view class="tips">每次下单会默认推荐该地址</view>
				</view>
				<view class="right">
					<u-switch v-model="addressData.isDefault" active-color="#fc4a5b"></u-switch>
				</view>
			</view>
		</view>
		<u-toast ref="uToast" />
		<u-picker mode="region" ref="uPicker" v-model="region_show" :zIndex="99999" :defaultRegion="defaultRegion" @confirm="confirmRegion"/>
		<button type="warning" shape="circle"  class="fs30 bottom-btn primarybtn" @click="submit">
			提交
		</button>
		<!-- 授权登录 -->
		<auth ref="auth"></auth>
	</view>
</template>

<script>
	import uSwitch from '@/pages_address/uview-ui/u-switch/u-switch.vue'
	import uPicker from '@/pages_address/uview-ui/u-picker/u-picker.vue'
	import { mapState } from 'vuex'
	export default {
		components: {
			uSwitch,
			uPicker
		},
		data() {
			return {
				from: '',
				defaultRegion:[],
				addressData: {
					consignee: '',
					province: '',
					city: '',
					area: '',
					address: '',
					mobile: '',
					isDefault: false
				},
				region_show:false,
				isLoading:false
			}
		},
		computed: {
			...mapState(['hasLogin', 'userInfo'])
		},
		onLoad(options) {
			this.from = options.from;
			
			let title = '新增收货地址';
			if (options.type === 'edit') {
				title = '编辑收货地址'
				this.addressData = JSON.parse(options.data)
				this.addressData.isDefault = this.addressData.isDefault === 1 ? true : false
				this.defaultRegion = [this.addressData.province, this.addressData.city, this.addressData.district];
			}
			this.manageType = options.type;
		},
		methods: {
			confirmRegion(e){
				this.addressData.province = e.province.label;
				this.addressData.city = e.city.label;
				this.addressData.area = e.area.label;
				this.addressData.merger_name = this.addressData.province + ',' + this.addressData.city + ',' + this.addressData.area;
				this.defaultRegion = [this.addressData.province, this.addressData.city, this.addressData.district];
				
			},
			submit(){
				console.log('userInfo', this.userInfo)
				if (!this.hasLogin) {
					this.$refs.auth.open()
					return
				}
				if (this.isLoading == true){
					return false;
				}
				let data = this.addressData;
				if (!data.consignee) {
					this.$api.msg('请填写收货人姓名')
					return;
				}
				if (!/(^1[0-9]{10}$)/.test(data.mobile)) {
					this.$api.msg('请输入正确的手机号码')
					return;
				}
				if (!data.province || !data.city || !data.area) {
					this.$api.msg('请选择所在区域')
					return;
				}
				if (!data.address) {
					this.$api.msg('请输入详细地址')
					return;
				}
				this.isLoading = true;
				console.log('addressData', this.addressData)
				this.$http.ajax({
					url: '/UserAddress/add',
					params: {
						'userId': this.userInfo.id,
						'consignee': this.addressData.consignee,
						'mobile': this.addressData.mobile,
						'province': this.addressData.province,
						'city': this.addressData.city,
						'area': this.addressData.area,
						'address': this.addressData.address,
						'isDefault': this.addressData.isDefault ? 1 : 0,
					},
					isOP: 0
				}).then((res) => {
					console.log('res', res)
					if (res.code == 200) {
						this.$api.msg(`地址${this.manageType=='edit' ? '修改': '添加'}成功`)
						setTimeout(() => {
							var pages = getCurrentPages(); //当前页
							var beforePage = pages[pages.length - 2]; //上个页面
							console.log('beforePage.route', beforePage.route)
							if (beforePage.route == 'pages_address/userAddress') {
								// beforePage.$vm.getAddress();
							}
							uni.navigateBack();
						}, 800)
					} else {
						this.$api.msg(res.msg)
					}
				}).finally(()=>{
					this.isLoading = false;
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
/deep/ .line {
	color: #c0c4cc;
	font-size: 28rpx;
}
.page-body {
	.top {
		background-color: #ffffff;
		border-top: solid 2rpx #f8f8f8;
		padding: 30rpx;
		.item {
			display: flex;
			font-size: 28rpx;
			line-height: 100rpx;
			align-items: center;
			border-bottom: solid 2rpx #f8f8f8;
			position: relative;
			.left {
				width: 150rpx;
			}
			input {
				text-align: left;
				font-size: 28rpx;
			}
		}
		.right-arrow{
			display: inline-block;
			width: 16rpx;
			height: 16rpx;
			border-top: 1rpx solid #666666;
			border-right: 1rpx solid #666666;
			transform: rotate(45deg);
			position: absolute;
			right: 0;
		}
		.address {
			padding: 20rpx 0;
			textarea {
				// width: 100%;
				height: 100rpx;
				line-height: 50rpx;
				padding: 0 40rpx;
				font-size: 28rpx;
			}
		}
		.site-clipboard {
			padding-right: 40rpx;
			textarea {
				// width: 100%;
				height: 150rpx;
				background-color: #f7f7f7;
				line-height: 60rpx;
				margin: 40rpx auto;
				padding: 20rpx;
			}
			.clipboard {
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 26rpx;
				color: #909399;
				height: 80rpx;
				.icon {
					margin-top: 6rpx;
					margin-left: 10rpx;
				}
			}
		}
		.isdefault {
			margin-top: 40rpx;
			display: flex;
			justify-content: space-between;
			.tips {
				font-size: 24rpx;
				color: #999999;
			}
			.right {
			}
		}
	}
	
}

</style>